package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.SysTenantDatasourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.SysTenantDatasourceForm;
import com.youlai.boot.factory.model.query.SysTenantDatasourceQuery;
import com.youlai.boot.factory.model.vo.SysTenantDatasourceVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 租户数据源配置前端控制层
 *
 * @author youlaitech
 * @since 2025-11-22 08:31
 */
@Tag(name = "租户数据源配置接口")
@RestController
@RequestMapping("/api/v1/sys-tenant-datasource")
@RequiredArgsConstructor
public class SysTenantDatasourceController  {

    private final SysTenantDatasourceService sysTenantDatasourceService;

    @Operation(summary = "租户数据源配置分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant-datasource:query')")
    public PageResult<SysTenantDatasourceVO> getSysTenantDatasourcePage(SysTenantDatasourceQuery queryParams ) {
        IPage<SysTenantDatasourceVO> result = sysTenantDatasourceService.getSysTenantDatasourcePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增租户数据源配置")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant-datasource:add')")
    public Result<Void> saveSysTenantDatasource(@RequestBody @Valid SysTenantDatasourceForm formData ) {
        boolean result = sysTenantDatasourceService.saveSysTenantDatasource(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取租户数据源配置表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant-datasource:edit')")
    public Result<SysTenantDatasourceForm> getSysTenantDatasourceForm(
        @Parameter(description = "租户数据源配置ID") @PathVariable Long id
    ) {
        SysTenantDatasourceForm formData = sysTenantDatasourceService.getSysTenantDatasourceFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改租户数据源配置")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant-datasource:edit')")
    public Result<Void> updateSysTenantDatasource(
            @Parameter(description = "租户数据源配置ID") @PathVariable Long id,
            @RequestBody @Validated SysTenantDatasourceForm formData
    ) {
        boolean result = sysTenantDatasourceService.updateSysTenantDatasource(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除租户数据源配置")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant-datasource:delete')")
    public Result<Void> deleteSysTenantDatasources(
        @Parameter(description = "租户数据源配置ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sysTenantDatasourceService.deleteSysTenantDatasources(ids);
        return Result.judge(result);
    }
}
